package com.poker.duiued;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.poker.iuious.yhyhjs.jklimj.User;
import com.poker.jkiuus.R;
import com.poker.jkiuus.uyrfgd.AppManager;
import com.poker.jkiuus.uyrfgd.DatabaseHelper;

import java.util.Locale;

public class UserAccountActivity extends AppCompatActivity {
    private TableRow user_info;
    private TableRow updatePws;
    private TableRow clearAccount;
    private TextView login_out;
    private String userName;
    private DatabaseHelper databaseHelper;
    private User user = new User();
    private Toast currentToast;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.useraccount);
        databaseHelper = new DatabaseHelper(this);

        userName = getIntent().getStringExtra("USER_NAME");
        user = databaseHelper.queryUserByNameOrEmail(userName);
        AppManager.addActivity(this);


        user_info = findViewById(R.id.user_info);
        user_info.setOnClickListener(v -> {
            Intent intent = new Intent(UserAccountActivity.this, UserAccount_InfoActivity.class);
            intent.putExtra("USER",user);
            intent.putExtra("USER_NAME",userName);
            startActivity(intent);
        });

        updatePws = findViewById(R.id.updatePws);
        updatePws.setOnClickListener(v -> {
            Intent intent = new Intent(UserAccountActivity.this, UserAccount_PwsActivity.class);
            intent.putExtra("USER",user);
            intent.putExtra("USER_NAME",userName);
            startActivity(intent);
        });

        clearAccount = findViewById(R.id.clearAccount);
        clearAccount.setOnClickListener(v -> {
            verification();
        });

        login_out = findViewById(R.id.login_out);
        login_out.setOnClickListener(v -> {
            Intent intent = new Intent(UserAccountActivity.this, LoginActivity.class);
            startActivity(intent);
            AppManager.finishAll();
        });

    }

    private void verification() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_verification, null);
        EditText etVerification = dialogView.findViewById(R.id.et_verification);
        ProgressBar progressBar = dialogView.findViewById(R.id.progressBar);
        progressBar.setVisibility(View.INVISIBLE);

        builder.setView(dialogView);
        builder.setTitle("Cancel your account\n");
        builder.setMessage("\nAre you sure you want to cancel your account?");
        builder.setPositiveButton("Confirm", null);
        builder.setNegativeButton("Cancel", null);
        builder.setCancelable(false);

        AlertDialog dialog = builder.create();
        dialog.setOnShowListener(dlg -> {
            Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
            positiveButton.setOnClickListener(view -> {
                String verification = etVerification.getText().toString().trim();

                if ("confirm".equals(verification.toLowerCase(Locale.ROOT))) {

                    progressBar.setVisibility(View.VISIBLE);

                    databaseHelper.remove(user.getName());
                    databaseHelper.clearChatHistory(String.valueOf(user.getId()));
                    databaseHelper.clearUserQueries(user.getId());

                    new Handler().postDelayed(() -> {
                        progressBar.setVisibility(View.GONE);
                        dialog.dismiss();
                        if (currentToast!=null){
                            currentToast.cancel();
                        }
                        currentToast = Toast.makeText(this, "The account has been successfully cancelled.", Toast.LENGTH_SHORT);
                        currentToast.show();
                        Intent intent = new Intent(UserAccountActivity.this, LoginActivity.class);
                        startActivity(intent);
                        AppManager.finishAll();
                    }, 2000);
                } else {
                    if (currentToast!=null){
                        currentToast.cancel();
                    }
                    currentToast = Toast.makeText(this, "Validation failed, please re-enter.", Toast.LENGTH_SHORT);
                    currentToast.show();
                    etVerification.setError("Validation failed, please re-enter.");
                    etVerification.setText("");
                }
            });

            Button negativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
            negativeButton.setOnClickListener(view -> dialog.dismiss());
        });

        dialog.show();
    }

}
